package com.lej.core.widget.drawable

import android.annotation.TargetApi
import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.TextPaint
import kotlin.math.min

/**
 * 文字Drawable
 */
class TextDrawable(
    context: Context,
    textSize: Float,
    textColor: Int,
    text: String
) : Drawable() {
    private val density: Float = context.resources.displayMetrics.density
    private val mTextPaint // 文字画笔
            : TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
    private var mTextSize = 0f
    private val mMeasureRect = Rect()
    private var mText: String? = null
    private var mIntrinsicWidth = 0
    private var mIntrinsicHeight = 0
    private var autoScale = false

    @TargetApi(5)
    private fun updateTextPaintDensity() {
        mTextPaint.density = density
    }

    override fun getIntrinsicWidth(): Int {
        return mIntrinsicWidth
    }

    override fun getIntrinsicHeight(): Int {
        return mIntrinsicHeight
    }

    override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
        super.setBounds(left, top, right, bottom)
    }

    override fun draw(canvas: Canvas) {
        if (mText == null || mText!!.isEmpty()) return
        val width = bounds.width()
        val height = bounds.height()
        if (width <= 0 || height <= 0) return
        mTextPaint.textSize = if (autoScale) min(width, height).toFloat() else mTextSize
        mTextPaint.getTextBounds(mText, 0, mText!!.length, mMeasureRect)
        val textWidth = mMeasureRect.width()
        val textHeight = mMeasureRect.height()
        if (textWidth <= 0 || textHeight <= 0) return
        canvas.save()
        if (autoScale) {
            val scale: Float
            val scaleWidth = width / textWidth.toFloat()
            val scaleHeight = height / textHeight.toFloat()
            scale = min(scaleWidth, scaleHeight)
            canvas.translate(bounds.centerX().toFloat(), bounds.centerY().toFloat())
            canvas.scale(scale, scale, 0f, 0f)
            canvas.translate(0f, textHeight * 0.5f)
            canvas.drawText(mText!!, 0f, -mMeasureRect.bottom.toFloat(), mTextPaint)
        } else {
            canvas.translate(bounds.centerX().toFloat(), bounds.centerY().toFloat())
            canvas.translate(0f, textHeight * 0.5f)
            canvas.drawText(mText!!, 0f, -mMeasureRect.bottom.toFloat(), mTextPaint)
        }
        canvas.restore()
    }

    override fun setAlpha(alpha: Int) {
        mTextPaint.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        mTextPaint.colorFilter = colorFilter
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }

    /**
     * 设置文字大小
     *
     * @param size 文字大小
     */
    fun setTextSize(size: Float) {
        if (mTextSize != size) {
            mTextSize = size
            mTextPaint.textSize = mTextSize
            if (mText == null) {
                mIntrinsicWidth = 0
                val metrics = mTextPaint.fontMetricsInt
                mIntrinsicHeight = metrics.bottom - metrics.top
            } else {
                mTextPaint.getTextBounds(mText, 0, mText!!.length, mMeasureRect)
                mIntrinsicWidth = mMeasureRect.width()
                mIntrinsicHeight = mMeasureRect.height()
            }
            invalidateSelf()
        }
    }

    /**
     * 设置文字颜色
     *
     * @param color 颜色
     */
    fun setTextColor(color: Int) {
        mTextPaint.color = color
        invalidateSelf()
    }

    /**
     * 设置文本
     *
     * @param text 文本
     */
    fun setText(text: String) {
        if (mText == null || mText != text) {
            mText = text
            mTextPaint.textSize = mTextSize
            mTextPaint.getTextBounds(mText, 0, mText!!.length, mMeasureRect)
            mIntrinsicWidth = mMeasureRect.width()
            mIntrinsicHeight = mMeasureRect.height()
            invalidateSelf()
        }
    }

    /**
     * 设置文字大小是否根据Bound自动缩放
     *
     * @param auto 自动缩放
     */
    fun setAutoScale(auto: Boolean) {
        if (autoScale != auto) {
            autoScale = auto
            invalidateSelf()
        }
    }

    init {
        mTextPaint.textAlign = Paint.Align.CENTER
        if (Build.VERSION.SDK_INT > 4) {
            updateTextPaintDensity()
        }
        setTextSize(textSize)
        setTextColor(textColor)
        setText(text)
    }
}